using System.Collections.Generic;
using UnityEngine;

public class BackManager : MonoBehaviour
{
	public List<IBackAction> backObjects = new List<IBackAction>();

	public bool RegisterObject(IBackAction backObject)
	{
		bool flag = false;
		for (int i = 0; i < backObjects.Count; i++)
		{
			if (backObjects[i] == backObject)
			{
				flag = true;
				break;
			}
		}
		if (!flag)
		{
			backObjects.Add(backObject);
			return true;
		}
		return false;
	}

	public bool UnregisterObject(IBackAction backObject)
	{
		for (int i = 0; i < backObjects.Count; i++)
		{
			if (backObjects[i] == backObject)
			{
				backObjects.RemoveAt(i);
				return true;
			}
		}
		return false;
	}

	private void Update()
	{
		if (UnityEngine.Input.GetKeyDown(KeyCode.Escape) && (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.WindowsEditor))
		{
			InvokeBackAction();
		}
	}

	private void InvokeBackAction()
	{
		int num = backObjects.Count - 1;
		while (true)
		{
			if (num >= 0)
			{
				if (!((Object)backObjects[num] == null))
				{
					break;
				}
				backObjects.RemoveAt(num);
				num--;
				continue;
			}
			return;
		}
		backObjects[num].OnBackAction();
	}
}
